package com.zhiwei.gateway.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 路由刷新服务(定时任务): org.springframework.cloud.netflix.zuul.RoutesEndpoint#reset()
 */
@Service
@Slf4j
public class RefreshRouteService {

    private final ApplicationEventPublisher publisher;

    private final RouteLocator routeLocator;

    @Autowired
    public RefreshRouteService(ApplicationEventPublisher publisher, RouteLocator routeLocator) {
        this.publisher = publisher;
        this.routeLocator = routeLocator;
    }

    /**
     * 路由刷新
     */
    public void refreshRoute() {
        RoutesRefreshedEvent routesRefreshedEvent = new RoutesRefreshedEvent(routeLocator);
        publisher.publishEvent(routesRefreshedEvent);
        log.info("Updating route configuration");
    }
}